package com.softserveinc.moviedbapp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.SupportActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;

import com.softserveinc.moviedbapp.R;
import com.softserveinc.moviedbapp.adapter.MovieListFragmentAdapter;
import com.softserveinc.moviedbapp.manager.DataManager;
import com.softserveinc.moviedbapp.provider.MovieListType;
import com.viewpagerindicator.TitlePageIndicator;

import java.util.ArrayList;
import java.util.List;

public class MovieListHostFragment extends Fragment {

	private static String TAG = MovieListHostFragment.class.getSimpleName();
	private static final boolean DEBUG = true;
	private View mMainFrameLayout;
	private View mPagerHolder;

	private static final int FIRST_PAGE = 1;
	private MovieListFragmentAdapter movieFragmentListAdapter;
	private ViewPager viewPager;
	private TitlePageIndicator indicator;
	private List<MovieListFragment> movieListFragments;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (DEBUG) {
			Log.d(TAG, "onCreate()");
		}
		setRetainInstance(true);
	}

	@Override
	public void onAttach(final SupportActivity activity) {
		super.onAttach(activity);
		if (DEBUG) {
			Log.d(TAG, "onAttach()");
			Log.d(TAG, "state: viewPager = " + viewPager);
			Log.d(TAG, "state: indicator = " + indicator);
			Log.d(TAG, "state: movieListFragments = " + movieListFragments);
			Log.d(TAG, "state: movieFragmentListAdapter = " + movieFragmentListAdapter);
		}
	}

	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (DEBUG) {
			Log.d(TAG, "onActivityCreated()");
		}
		mMainFrameLayout = getActivity().findViewById(R.id.mainFrameLayout);
		mPagerHolder = getActivity().findViewById(R.id.pager_holder);

		viewPager = (ViewPager) mPagerHolder.findViewById(R.id.pager);

		if (viewPager.getAdapter() == null) {
			if (DEBUG) {
				Log.e(TAG, " going to set adapter to pager");
			}
			indicator = (TitlePageIndicator) mPagerHolder.findViewById(R.id.indicator);
			if (movieListFragments == null) {
				if (DEBUG) {
					Log.d(TAG, "movieListFragments == null");
				}
				movieListFragments = instantiateFragments();
			}
			indicator.setOnPageChangeListener(new OnPageChangeListener() {

				@Override
				public void onPageSelected(final int position) {
					Log.d(TAG, "------------------------------------");

					if (DEBUG) {
						Log.d(TAG, "onPageSelected() position " + position);
					}
					MovieListFragment curFragment = movieListFragments.get(position);
					MovieListType listType = MovieListType.fromValue(position);
					if (listType == MovieListType.RELEASE_DATE) {
						curFragment.setStartupList(true);
					}
					DataManager dataManager = curFragment.getDataMananger();
					Log.d(TAG, "datananager is not null " + (dataManager != null) + " for " + listType);
					Log.d(TAG, "adapter is not empty " + (curFragment.isAdapterEmpty()));
					if (dataManager != null && curFragment.isAdapterEmpty()) {
						if (DEBUG) {
							Log.d(TAG, "going to get the startup page for the list " + listType);
						}
						dataManager.provideMovieList(listType, null);
					}
				}

				@Override
				public void onPageScrolled(final int position, final float positionOffset,
						final int positionOffsetPixels) {
					if (DEBUG) {
						Log.d(TAG, "onPageScrolled() position " + position + " positionOffset " + positionOffset
								+ " positionOffsetPixels " + positionOffsetPixels);
					}
					if (positionOffsetPixels == 0) {
						MovieListType fragmentType = MovieListType.fromValue(position);
						notifyList(fragmentType);
					}
				}

				@Override
				public void onPageScrollStateChanged(final int state) {
					if (DEBUG) {
						Log.d(TAG, "onPageScrollStateChanged");
						switch (state) {
						case 0:
							Log.d(TAG, "SCROLL_STATE_IDLE");
							break;
						case 1:
							Log.d(TAG, "SCROLL_STATE_DRAGGING");
							break;
						case 2:
							Log.d(TAG, "SCROLL_STATE_SETTLING");
							break;
						default:
							Log.d(TAG, "-=UNKNOWN=-");
							break;
						}
					}
				}
			});
			movieFragmentListAdapter = new MovieListFragmentAdapter(getSupportFragmentManager(), movieListFragments,
					getResources());
			viewPager.setAdapter(movieFragmentListAdapter);
			indicator.setViewPager(viewPager, FIRST_PAGE);
		}
		/* hide the main container */
		mMainFrameLayout.setVisibility(View.GONE);
		mPagerHolder.setVisibility(View.VISIBLE);
	}

	@Override
	public void onResume() {
		if (DEBUG) {
			Log.d(TAG, "onResume()");
		}
		/* hide the main container */
		mPagerHolder.setVisibility(View.VISIBLE);
		mMainFrameLayout.setVisibility(View.GONE);
		super.onResume();
	}

	@Override
	public void onPause() {
		if (DEBUG) {
			Log.d(TAG, "onPause()");
		}
		/* show the main container */
		mPagerHolder.setVisibility(View.GONE);
		mMainFrameLayout.setVisibility(View.VISIBLE);
		super.onPause();
	}

	private List<MovieListFragment> instantiateFragments() {
		if (DEBUG) {
			Log.d(TAG, "instantiateFragments()");
		}
		List<MovieListFragment> fragments = new ArrayList<MovieListFragment>();

		MovieListFragment fragment = new MovieListFragment(MovieListType.TITLE);
		fragments.add(fragment);

		fragment = new MovieListFragment(MovieListType.RELEASE_DATE);
		fragments.add(fragment);

		fragment = new MovieListFragment(MovieListType.RATING);
		fragments.add(fragment);
		return fragments;
	}

	private void notifyList(final MovieListType fragmentType) {
		switch (fragmentType) {
		case TITLE:
			movieListFragments.get(MovieListType.TITLE.getListTypeInt()).notifyAdapter(true);
			movieListFragments.get(MovieListType.RELEASE_DATE.getListTypeInt()).notifyAdapter(false);
			movieListFragments.get(MovieListType.RATING.getListTypeInt()).notifyAdapter(false);
			break;
		case RELEASE_DATE:
			movieListFragments.get(MovieListType.TITLE.getListTypeInt()).notifyAdapter(false);
			movieListFragments.get(MovieListType.RELEASE_DATE.getListTypeInt()).notifyAdapter(true);
			movieListFragments.get(MovieListType.RATING.getListTypeInt()).notifyAdapter(false);
			break;
		case RATING:
			movieListFragments.get(MovieListType.TITLE.getListTypeInt()).notifyAdapter(false);
			movieListFragments.get(MovieListType.RELEASE_DATE.getListTypeInt()).notifyAdapter(false);
			movieListFragments.get(MovieListType.RATING.getListTypeInt()).notifyAdapter(true);
			break;
		default:
			if (DEBUG) {
				Log.d(TAG, "UNKNOWN");
			}
			break;
		}
	}
}
